package com.situ.ymall.mapper;

import com.situ.ymall.entity.Goods;
import com.situ.ymall.entity.GoodsPicture;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface GoodsPictureMapper {
    @Delete({
        "delete from goods_picture",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer id);

    @Insert({"<script>",
        "insert into goods_picture (goods_id,name)",
        "values ",
        "<foreach item='item' index='index' collection='pictures'  separator=','>",
        "(#{id},#{item.name})",
        "</foreach>",
        "</script>"
    })
    int batchInsert(Goods goods);

    @Select({
        "select",
        "id, goods_id, name",
        "from goods_picture",
        "where id = #{id,jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="goods_id", property="goodsId", jdbcType=JdbcType.INTEGER),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR)
    })
    GoodsPicture selectByPrimaryKey(Integer id);

    @Select({
        "select",
        "id, goods_id, name",
        "from goods_picture"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="goods_id", property="goodsId", jdbcType=JdbcType.INTEGER),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR)
    })
    List<GoodsPicture> selectAll();

    @Update({
        "update goods_picture",
        "set goods_id = #{goodsId,jdbcType=INTEGER},",
          "name = #{name,jdbcType=VARCHAR}",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(GoodsPicture record);

    @Select({"select * from goods_picture where goods_id = #{id}"})
    List<GoodsPicture> selectByGoodsId(Integer id);

    @Delete("delete from goods_picture where goods_id = #{id}")
    int deleteByGoodsId(Integer id);

}